package com.py.androidUtils.UI.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.py.androidUtils.UI.EditTypeActivity;
import com.py.androidUtils.UI.adapter.BelongCategoryAdapter;
import com.py.androidUtils.UI.model.BelongCategoryModel;
import com.readyidu.app.R;

public class BelongCategoryView extends Dialog {
	private GridView gridView;
	private ImageView close;
	private TextView determine;
	private BelongCategoryAdapter belongCategoryAdapter;
	private String[] types = { "美食", "创意", "颜值", "漫画", "音乐", "搞笑", "摄影", "其他", "图趣" };
	private Context mContext;
	private String type = "";

	public BelongCategoryView(Context context) {
		super(context);
		initView(context);
		this.mContext = context;
	}

	public void initView(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.common_type_diaolge, null);
		close = (ImageView) view.findViewById(R.id.close);
		gridView = (GridView) view.findViewById(R.id.gridView);
		determine = (TextView) view.findViewById(R.id.determine);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		addContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}

	public void showDiaolge() {
		List<BelongCategoryModel> list = new ArrayList<BelongCategoryModel>();
		for (int i = 0; i < types.length; i++) {
			BelongCategoryModel belongCategoryModel = new BelongCategoryModel();
			belongCategoryModel.setTitle(types[i]);
			list.add(belongCategoryModel);
		}
		belongCategoryAdapter = new BelongCategoryAdapter(getContext());
		gridView.setAdapter(belongCategoryAdapter);
		belongCategoryAdapter.setList(list);
		setCancelable(false);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				for (int i = 0; i < belongCategoryAdapter.getListData().size(); i++) {
					((BelongCategoryModel) belongCategoryAdapter.getListData().get(i)).setSelect(false);
				}
				((BelongCategoryModel) belongCategoryAdapter.getListData().get(position)).setSelect(true);
				belongCategoryAdapter.notifyDataSetChanged();
				if (((BelongCategoryModel) belongCategoryAdapter.getListData().get(position)).getTitle().equals("其他")) {
					Intent intent = new Intent(mContext, EditTypeActivity.class);
					((Activity) mContext).startActivityForResult(intent, 2);
					((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_out);
					dismiss();
				} else {
					type = ((BelongCategoryModel) belongCategoryAdapter.getListData().get(position)).getTitle();
				}
			}
		});
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		determine.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
				typeOnClickListener.getType(type);
			}
		});
		show();
	}

	public interface TypeOnClickListener {
		void getType(String type);
	}

	public TypeOnClickListener typeOnClickListener;

	public void setTypeOnClickListener(TypeOnClickListener typeOnClickListener) {
		this.typeOnClickListener = typeOnClickListener;
	}
}
